package com.cetcs.kmga.dataManager.controller;

import com.cetcs.kmga.common.*;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.IpSettingVo;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.service.dataManager.IpSettingManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import javax.validation.Valid;
import java.util.List;

/**
 * Ip集合配置控制器
 * Created by root on 2017/9/21.
 */
@RestController
@RequestMapping("/ipSettingManage")
public class IpSettingManageController extends BaseController {

    @Autowired
    private IpSettingManageService ipSettingManagService;
    /**
     * 新增Ip集合
     */
    @LogAnnotation(module = "数据资源管理-告警管理-参数配置", logType = LogTypeEnum.业务日志, description = "新增Ip集合", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/addIpSetting", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse addIpSetting(@Valid @RequestBody IpSettingVo vo, BindingResult bindingResult) {
        try{
            String checkMessage = vo.checkInput();
            if(!StringUtils.isEmpty(checkMessage)){
                return this.error(Constant.SYSTEM_EXCEPTION,checkMessage);
            }
            ipSettingManagService.addIpSetting(vo);
            return this.ok(1);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION,e.getMessage());
        }
    }

    /**
     * 修改Ip集合
     */
    @LogAnnotation(module = "数据资源管理-告警管理-参数配置", logType = LogTypeEnum.业务日志, description = "修改Ip集合", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/modifyIpSetting/{id}", method = RequestMethod.PUT)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse modifyIpSetting(@PathVariable("id") int id,@RequestBody IpSettingVo vo) {
        try{
            String checkMessage = vo.checkInput();
            if(!StringUtils.isEmpty(checkMessage)){
                return this.error(Constant.SYSTEM_EXCEPTION,checkMessage);
            }
            vo.setId(id);
            ipSettingManagService.modifyIpSetting(vo);
            return this.ok(1);
        }catch (Exception e){
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 分页Ip集合
     */
    @LogAnnotation(module = "数据资源管理-告警管理-参数配置", logType = LogTypeEnum.业务日志, description = "获取Ip集合列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findIpSetting", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse findIpSetting(@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
                                 @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                 @RequestParam(value = "keyword", required = false) String keyword) {
        try {
            PageList<IpSettingVo> pageList = ipSettingManagService.findIpSetting(pageNumber, pageSize,  keyword);
            Page page = new Page(pageList);
            page.setPageSize(pageList.getPaginator().getLimit());
            return this.ok(page);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 根据ID删除Ip集合
     */
    @LogAnnotation(module = "数据资源管理-告警管理-参数配置", logType = LogTypeEnum.业务日志, description = "删除Ip集合", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/removeIpSettingById/{id}", method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse removeIpSettingById(@PathVariable("id") String id) {
        try {
            ipSettingManagService.removeIpSettingById(id);
            return this.ok(1);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 根据ID批量删除Ip集合
     */
    @LogAnnotation(module = "数据资源管理-告警管理-参数配置", logType = LogTypeEnum.业务日志, description = "批量删除Ip集合", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/batchRemoveIpSetting", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse batchRemoveIpSetting(@RequestBody Data data) {
        try {
            List<String> ids  = (List<String>) data.get("ids");
            ipSettingManagService.batchRemoveIpSetting(ids);
            return this.ok(1);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }
    /**
     * 查询所有Ip集合
     */
    @LogAnnotation(module = "数据资源管理-告警管理-参数配置", logType = LogTypeEnum.业务日志, description = "查询所有Ip集合列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findAllIpSetting", method = RequestMethod.GET)
    @ResponseStatus(HttpStatus.OK)
    public BaseResponse findAllIpSetting(@RequestParam(value = "keyword", required = false) String keyword) {
        try {
            List<IpSettingVo> pageList = ipSettingManagService.findAllIpSetting(keyword);

            return this.ok(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }
}
